import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { map } from 'rxjs/operators';

import { LastUrlService, GetLastURL } from '../../services/last-url.service';
import { TargetDataList, StatusDataList, ErrorCodeDataList } from './mock-select';

@Component({
    selector: 'm-pc-tab',
    templateUrl: './pc-tab.component.html',
    styleUrls: ['./pc-tab.component.scss']
})
export class PcTabComponent implements OnInit {
    pcButtons = [
        { link: '/logs/pc/exception', name: '终端异常日志' },
        { link: '/logs/pc/upgrade', name: '终端更新日志' }
    ];
    targetList = TargetDataList;
    statusList = StatusDataList;
	errCodeList = ErrorCodeDataList;

    constructor(private router: Router,
        private route: ActivatedRoute,
        private urlService: LastUrlService) { }

    activePcBtn: string;
    activeAreaID: number;
    activeErrCode: number;
    activeTargetID: number;
    activeStatusID: number;
    searchKey: string = '';

    areaList$ = this.route.data.pipe(map(data => data['zone_list']));

    ngOnInit() {
        this.urlService.getURL().subscribe(url => {
            switch (url) {
                case 'exception':
                    this.activePcBtn = this.pcButtons[0].link;
                    break;
                case 'upgrade':
                    this.activePcBtn = this.pcButtons[1].link;
                    break;
            }
        });

        this.route.queryParamMap.subscribe(data => {
            this.searchKey = data.get('keywords') || '';
            this.activeAreaID = +(data.get('area_id') || 0);
            this.activeErrCode = +(data.get('error_code') || 0);
            this.activeTargetID = +(data.get('target_id') || 0);
            this.activeStatusID = +(data.get('status') || 0);
        });
    }

    onSearchKey(key: string, exec: boolean) {
        if (key == this.searchKey) {
            return;
        }
        this.searchKey = key;
        if (!exec) {
            return;
        }
        this.startSearch();
    }

    onActiveAreaID(id: number) {
        if (this.activeAreaID == id) {
            return;
        }
        this.activeAreaID = id;
        this.startSearch();
    }

    onActiveErrCode(id: number) {
        if (this.activeErrCode == id) {
            return;
        }
        this.activeErrCode = id;
        this.startSearch();
    }

    onActiveTargetID(id: number) {
        if (this.activeTargetID == id) {
            return;
        }
        this.activeTargetID = id;
        this.startSearch();
    }

    onActiveStatusID(id: number) {
        if (this.activeStatusID == id) {
            return;
        }
        this.activeStatusID = id;
        this.startSearch();
    }

    startSearch() {
        // gen params
        var params = { page: 1 };
        if (this.searchKey.length != 0) {
            params['keywords'] = this.searchKey.trim();
        }
        if (this.activeAreaID != 0) {
            params['area_id'] = this.activeAreaID;
        }
        if (this.activeErrCode != 0) {
            params['error_code'] = this.activeErrCode;
        }
        if (this.activeTargetID != 0) {
            params['target_id'] = this.activeTargetID;
        }
        if (this.activeStatusID != 0) {
            params['status'] = this.activeStatusID;
        }
        this.router.navigate([GetLastURL(this.route)], {
            relativeTo: this.route,
            queryParams: params
        });
    }
};
